import { ElMessageBox } from 'element-plus'
import { nodeinfo } from '@/stores/nodeinfo'
import { storeInfo } from '@/stores/storeinfo'

export const useFileDelete = () => {
    const nodeinfoStore = nodeinfo()
    const storeinfo = storeInfo()

    // 删除文件
    const deleteNode = async (node: any, item: any, classToFile: any) => {
        console.log("删除时获取nodeId和stage", node.nodeId, "，", item.stage)
        const nodeId = node.nodeId
        const stage = item.stage
        const classId = classToFile.id
        await nodeinfoStore.deleteNode({ nodeId, stage, classId })
    }

    // 确认删除文件
    const confirmDeleteNode = (node: any, item: any, classToFile: any, stageList: any) => {
        ElMessageBox.confirm(
            `确定要删除文件 "${node.title}" 吗？`,
            '警告',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(async () => {
            await deleteNode(node, item, classToFile)
            // 从UI中立即移除该节点
            for (const stage of Object.values(stageList.value)) {
                if (stage.nodes) {
                    const index = stage.nodes.findIndex(n => n.nodeId === node.nodeId)
                    if (index !== -1) {
                        stage.nodes.splice(index, 1)
                        break
                    }
                }
            }
        }).catch(() => {
            // 用户点击了取消
            console.log('取消删除')
        })
    }

    return {
        deleteNode,
        confirmDeleteNode
    }
}